package E10;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 史祥连
 * @Date 2023/12/8 12:22
 * @Version 1.0
 */
public class UploadFile {

    private static String type ="release";

    private static String E10_API_URL_BASE_test = "https://api.yunteams.cn";
    private static String E10_API_URL_BASE_release = "https://api.mulinquan.cn";
    private static String E10_API_userid_test = "5116064451991577795";
    private static String E10_API_userid_release = "3086018350865928585";

    private static String app_key_release = "25fd00a1d41fe08be87a6dc9d7f28dc9";
    private static String app_secret_release = "bc21264099eac342e1f3b231877b969b";
    private static String Corpid_release = "b1bf4c491e3d2096ca0c657083839836";

    private static String app_key_test = "1a2f20f0773a839f43926227ad362dad";
    private static String Corpid_test = "6caeb52680ecddf055e83508c2f84371";
    private static String app_secret_test = "7e09d847c36d00bd82b9646e497c18ca";

    public static void main(String[] args) {

        String accessToken = loadAccessToken();
        String s = uploadAttachment(accessToken
                , new File("C:\\Users\\LENOVO\\Pictures\\Camera Roll\\shxl1225.png")
                , "shxl1225"
                , "10"
                , ""
                ,null
                ,null
        );

        System.out.println("s = " + s);
    }


    public static String loadAccessToken(){
        //https://weapp.e-cology.com.cn/openserver/oauth2/authorize?corpid=f65afca70cdab51533e1df83513b01ac&response_type=code&state=xifei

        String url = E10_API_URL_BASE_test+"/oauth2/authorize";
        // String url = "https://api.eteams.cn/oauth2/authorize";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("corpid", Corpid_test);
        paramMap.put("response_type", "code");
        paramMap.put("state", "123123");
        String result = HttpUtil.get(url, paramMap);
        System.out.println("1.1loadAccessToken-code-url::" + url);
        System.out.println("1.2loadAccessToken-code-RESP::" + result);
        ApiAccessTokenDTO dto = JsonHelper.parseJsonStringToObject(result, ApiAccessTokenDTO.class);
        String code = dto != null && dto.readIsSuccess()? dto.getCode() : "";
        System.out.println("1.3loadAccessToken-code-VALUE::" + code);
        String accessToken = null;
        if(!JsonHelper.isNullOrEmpty(code)){
//            TODO:正式
            url = E10_API_URL_BASE_test + "/oauth2/access_token";
//            TODO:测试
            // url = "https://api.eteams.cn/oauth2/access_token";
            paramMap = new HashMap<>();
            paramMap.put("app_key", app_key_test);
            paramMap.put("app_secret", app_secret_test);
            paramMap.put("grant_type", "authorization_code");
            paramMap.put("code", code);
            result = HttpUtil.post(url, paramMap);
            System.out.println("2.1loadAccessToken-token-url::" + url);
            System.out.println("2.2loadAccessToken-token-RESP::" + result);
            ApiAccessTokenDTO dto2 = JsonHelper.parseJsonStringToObject(result, ApiAccessTokenDTO.class);
            accessToken = dto2 != null && dto2.readIsSuccess()? dto2.getAccessToken() : "";
            System.out.println("2.3loadAccessToken-token-VALUE::" + accessToken);
        }
        return accessToken;
    }

    private static String uploadAttachment(String accessToken, File file, String fileName, String secret_years, String secret_level,String module,String refId){
        //https://api.eteams.cn/api/file/v2/common/upload
        String url = E10_API_URL_BASE_test + "/api/file/v2/common/upload?access_token=" + accessToken;
//        TODO：测试
        // String url = "https://api.eteams.cn/api/file/v2/common/upload?access_token=" + accessToken;

        try{
            HashMap<String, Object> paramMaps = new HashMap<>();
            paramMaps.put("name", "正文.doc".equals(fileName)?fileName:file.getName());
            paramMaps.put("file", file);
            paramMaps.put("userid", E10_API_userid_test);
            System.out.println(file);
            // 制定文件存储目录
            paramMaps.put("folderId","301");
            paramMaps.put("secretLevel",secret_level);
            paramMaps.put("secretLevelValidity",secret_years);
            if (module != null) {
                paramMaps.put("module",module);
            }
            if (refId != null) {
                paramMaps.put("refId",refId);
            }
            System.out.println(paramMaps);
            // HttpResponse response=null;
            HttpResponse response = HttpRequest.post(url)
                    .form(paramMaps)
                    .execute();
            int status = response.getStatus();
            System.out.println("uploadAttachment请求响应状态码:" + status);
            String body = response.body();
            System.out.println(body);
            ApiFileUploadRespDTO respDTO = JsonHelper.parseJsonStringToObject(body, ApiFileUploadRespDTO.class);
            String fileid = "";
            if(respDTO != null && respDTO.getData() != null){
                System.out.println("fileid=" + respDTO.getData().getFileid());
                fileid = respDTO.getData().getFileid();
            }
            //        JSONObject jsonObject = JSONObject.parseObject(body);
            return fileid;
        }catch(Exception e){
            // e.printStackTrace();
            System.out.println("附件上传异常了===》");
            return "";
        }


    }
    //https://open-doc.eteams.cn/web/#/1?page_id=223
    //文件上传 接口响应数据data节
    public static class ApiFileUploadRespDTO{
        private ApiFileUploadRespMessageDTO message;
        private ApiFileUploadRespDataDTO data;

        public boolean readIsSuccess(){
            return message != null && "0".equals(message.getErrcode());
        }

        public ApiFileUploadRespMessageDTO getMessage() {
            return message;
        }

        public void setMessage(ApiFileUploadRespMessageDTO message) {
            this.message = message;
        }

        public ApiFileUploadRespDataDTO getData() {
            return data;
        }

        public void setData(ApiFileUploadRespDataDTO data) {
            this.data = data;
        }
    }
    //接口响应数据data节
    public static class ApiFileUploadRespDataDTO {
        private String fileid;
        private String docId;
        private String name;
        private Long size;
        private String sizeUnit;
        private String extName;
        private String type;
        private Boolean img;
        private String uploadUser;
        private String uploadUserName;
        private Long uploadTime;
        private Boolean ban;

        public String getFileid() {
            return fileid;
        }

        public void setFileid(String fileid) {
            this.fileid = fileid;
        }

        public String getDocId() {
            return docId;
        }

        public void setDocId(String docId) {
            this.docId = docId;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Long getSize() {
            return size;
        }

        public void setSize(Long size) {
            this.size = size;
        }

        public String getSizeUnit() {
            return sizeUnit;
        }

        public void setSizeUnit(String sizeUnit) {
            this.sizeUnit = sizeUnit;
        }

        public String getExtName() {
            return extName;
        }

        public void setExtName(String extName) {
            this.extName = extName;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public Boolean getImg() {
            return img;
        }

        public void setImg(Boolean img) {
            this.img = img;
        }

        public String getUploadUser() {
            return uploadUser;
        }

        public void setUploadUser(String uploadUser) {
            this.uploadUser = uploadUser;
        }

        public String getUploadUserName() {
            return uploadUserName;
        }

        public void setUploadUserName(String uploadUserName) {
            this.uploadUserName = uploadUserName;
        }

        public Long getUploadTime() {
            return uploadTime;
        }

        public void setUploadTime(Long uploadTime) {
            this.uploadTime = uploadTime;
        }

        public Boolean getBan() {
            return ban;
        }

        public void setBan(Boolean ban) {
            this.ban = ban;
        }
        //        "data":{
//            "fileid":"891148419246383104",
//                    "name":"aa.png",
//                    "size":3705,
//                    "sizeUnit":"B",
//                    "extName":"png",
//                    "type":"image/png",
//                    "img":true,
//                    "uploadUser":"880100882807193609",
//                    "uploadUserName":"????",
//                    "uploadTime":1690686660705,
//                    "ban":false,
//                    "docId":"4369068666072763109"
//        }
    }

    //接口响应数据message节
    public static class ApiFileUploadRespMessageDTO{
        private String errcode;
        private String errmsg;

        public String getErrcode() {
            return errcode;
        }

        public void setErrcode(String errcode) {
            this.errcode = errcode;
        }

        public String getErrmsg() {
            return errmsg;
        }

        public void setErrmsg(String errmsg) {
            this.errmsg = errmsg;
        }
    }

    //数据库表字段配置
    public static class DbTableFieldDTO{
        //老OA系统中中字段名，大写
        private String name;
        //E10中EB建模表单里的字段名
        private String nameNew;
        private String comment;
        private String fieldType;
        private Object value;
        //e-Builder表单中的fieldId
        private String ebFieldId;

        public DbTableFieldDTO(){

        }


        public DbTableFieldDTO(String name, String nameNew, String comment, String fieldType){
            this.name = name;
            this.nameNew = nameNew;
            this.comment = comment;
            this.fieldType = fieldType;
        }
        public DbTableFieldDTO(String name, String comment, String fieldType){
            this.name = name;
            this.nameNew = name;
            this.comment = comment;
            this.fieldType = fieldType;
        }

        //是否是附件字段
        public boolean readIsFieldTypeAttachment(){
            return "attachment".equalsIgnoreCase(this.fieldType);
        }

        public String getName(){
            return this.name;
        }
        public void setName(String name){
            this.name = name;
        }
        public String getComment(){
            return this.comment;
        }
        public void setComment(String comment){
            this.comment = comment;
        }
        public String getFieldType(){
            return this.fieldType;
        }
        public void setFieldType(String fieldType){
            this.fieldType = fieldType;
        }

        public String getNameNew() {
            return nameNew;
        }

        public void setNameNew(String nameNew) {
            this.nameNew = nameNew;
        }

        public Object getValue() {
            return value;
        }

        public void setValue(Object value) {
            this.value = value;
        }

        public String getEbFieldId() {
            return ebFieldId;
        }

        public void setEbFieldId(String ebFieldId) {
            this.ebFieldId = ebFieldId;
        }
    }

    //[表单]表单数据保存接口 saveFormData请求参数
    public static class ApiEbSaveFormDataDTO{
        private ApiEbSaveFormDataHeaderDTO header;
        private ApiEbSaveFormDataDatasDTO datas;

//        public ApiEbSaveFormDataDTO putFieldValue(ApiEbSaveFormDataDatasFieldValueDTO fieldValueDTO){
//            if(this.datas != null){
//                this.datas.putFieldValue(fieldValueDTO);
//            }
//            return this;
//        }

        public ApiEbSaveFormDataDTO putFieldValue(String fieldId, String v){
            if(this.datas != null){
                if ("classification".equals(fieldId)) {
                    this.datas.setClassification(Integer.parseInt(v));
                } else if ("classificationExpire".equals(fieldId)) {
                    this.datas.setClassificationExpire(v);
                } else {
                    this.datas.putFieldValue(fieldId, v);

                }
            }
            return this;
        }



        public ApiEbSaveFormDataDTO initBy(String objId, String listId){
            this.header = new ApiEbSaveFormDataHeaderDTO();
            this.header.initBy(objId);
            this.datas = new ApiEbSaveFormDataDatasDTO();
            this.datas.initBy(listId);
            return this;
        }

        public ApiEbSaveFormDataHeaderDTO getHeader() {
            return header;
        }

        public void setHeader(ApiEbSaveFormDataHeaderDTO header) {
            this.header = header;
        }

        public ApiEbSaveFormDataDatasDTO getDatas() {
            return datas;
        }

        public void setDatas(ApiEbSaveFormDataDatasDTO datas) {
            this.datas = datas;
        }
    }

    //[表单]表单数据保存接口 datas
    public static class ApiEbSaveFormDataDatasDTO{
        //        密级
        private int classification;
        //        密级年限
        private String classificationExpire;
        private ApiEbSaveFormDataDatasMainTableDTO mainTable;
        private ApiEbSaveFormDataDatasOperationinfoDTO operationinfo;
//        private List<ApiEbSaveFormDataDatasFieldValueDTO> dataDetails;

        public ApiEbSaveFormDataDatasDTO initBy(String listId){
            this.mainTable = new ApiEbSaveFormDataDatasMainTableDTO().initBy(listId);
            this.operationinfo = new ApiEbSaveFormDataDatasOperationinfoDTO().initDemo();
//            this.dataDetails = new ArrayList<>();
            return this;
        }

//        public ApiEbSaveFormDataDatasDTO putFieldValue(ApiEbSaveFormDataDatasFieldValueDTO fieldValueDTO){
//            if(this.dataDetails == null){
//                this.dataDetails = new ArrayList<>();
//            }
//            this.dataDetails.add(fieldValueDTO);
//            return this;
//        }

        public ApiEbSaveFormDataDatasDTO putFieldValue(String fieldId, String v) {
//            ApiEbSaveFormDataDatasFieldValueDTO fieldValueDTO = new ApiEbSaveFormDataDatasFieldValueDTO();
//            fieldValueDTO.putValue(fieldId, v);
//            this.putFieldValue(fieldValueDTO);
            this.mainTable.put(fieldId, v);
            return this;
        }


        public ApiEbSaveFormDataDatasMainTableDTO getMainTable() {
            return mainTable;
        }

        public void setMainTable(ApiEbSaveFormDataDatasMainTableDTO mainTable) {
            this.mainTable = mainTable;
        }

        public ApiEbSaveFormDataDatasOperationinfoDTO getOperationinfo() {
            return operationinfo;
        }

        public void setOperationinfo(ApiEbSaveFormDataDatasOperationinfoDTO operationinfo) {
            this.operationinfo = operationinfo;
        }
        public int getClassification() {
            return classification;
        }

        public void setClassification(int classification) {
            this.classification = classification;
        }

        public String getClassificationExpire() {
            return classificationExpire;
        }

        public void setClassificationExpire(String classificationExpire) {
            this.classificationExpire = classificationExpire;
        }

//        public List<ApiEbSaveFormDataDatasFieldValueDTO> getDataDetails() {
//            return dataDetails;
//        }
//
//        public void setDataDetails(List<ApiEbSaveFormDataDatasFieldValueDTO> dataDetails) {
//            this.dataDetails = dataDetails;
//        }
    }
    //[表单]表单数据保存接口 datas mainTable
    public static class ApiEbSaveFormDataDatasMainTableDTO extends HashMap<String, Object>{
//        private String id;

        public ApiEbSaveFormDataDatasMainTableDTO initBy(String id){
//            this.id = id;
            this.put("id", id);
            return this;
        }

//        public String getId() {
//            return id;
//        }
//
//        public void setId(String id) {
//            this.id = id;
//        }
    }
    //[表单]表单数据保存接口 datas dataDetails
//    {
//        "fieldId": "name",
//            "fieldValue": [
//        "西瓜"
//          ]
//    }
    public static class ApiEbSaveFormDataDatasFieldValueDTO{
        private String fieldId;
        private List<String> fieldValue;

        public ApiEbSaveFormDataDatasFieldValueDTO putValue(String fieldId, String v){
            this.fieldId = fieldId;
            if(this.fieldValue == null){
                this.fieldValue = new ArrayList<>();
            }
            this.fieldValue.add(v == null ? "" : v);
            return this;
        }

        public String getFieldId() {
            return fieldId;
        }

        public void setFieldId(String fieldId) {
            this.fieldId = fieldId;
        }

        public List<String> getFieldValue() {
            return fieldValue;
        }

        public void setFieldValue(List<String> fieldValue) {
            this.fieldValue = fieldValue;
        }
    }
    //[表单]表单数据保存接口 datas operationinfo
//        "operationinfo": {
//                "checkRight": "n",
//                "doAction": "n",
//                "optionNoFindIgnore": "false"
//        },
    public static class ApiEbSaveFormDataDatasOperationinfoDTO{
        private String checkRight;
        private String doAction;
        private String optionNoFindIgnore;

        public ApiEbSaveFormDataDatasOperationinfoDTO initDemo(){
            this.checkRight = "n";
            this.doAction = "n";
            this.optionNoFindIgnore = "false";
            return this;
        }

        public String getCheckRight() {
            return checkRight;
        }

        public void setCheckRight(String checkRight) {
            this.checkRight = checkRight;
        }

        public String getDoAction() {
            return doAction;
        }

        public void setDoAction(String doAction) {
            this.doAction = doAction;
        }

        public String getOptionNoFindIgnore() {
            return optionNoFindIgnore;
        }

        public void setOptionNoFindIgnore(String optionNoFindIgnore) {
            this.optionNoFindIgnore = optionNoFindIgnore;
        }
    }


    //[表单]表单数据保存接口 header
//   "header": {
//        "objId": 887057840673996801
//    }
    public static class ApiEbSaveFormDataHeaderDTO{
        //表单id
        private String objId;

        public ApiEbSaveFormDataHeaderDTO initBy(String objId){
            this.objId = objId;
            return this;
        }

        public String getObjId() {
            return objId;
        }

        public void setObjId(String objId) {
            this.objId = objId;
        }
    }
    //接口响应数据
    public static class ApiEbRespAaDTO{
        private ApiEbRespMessageDTO message;
        private ApiEbRespDataAaDTO data;

        public boolean readIsSuccess(){
            return message != null && "0".equals(message.getCode());
        }

        public ApiEbRespMessageDTO getMessage() {
            return message;
        }

        public void setMessage(ApiEbRespMessageDTO message) {
            this.message = message;
        }

        public ApiEbRespDataAaDTO getData() {
            return data;
        }

        public void setData(ApiEbRespDataAaDTO data) {
            this.data = data;
        }
    }
    //接口响应数据message节
    public static class ApiEbRespMessageDTO{
        private String code;
        private String msg;

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }
        //        "message": {
//            "code": "0",
//                    "msg": "success"
//        },
    }
    //接口响应数据data节
    public static class ApiEbRespDataAaDTO{
        private List<ApiEbFormFieldDTO> mainFields;
        private List<ApiEbFormFieldDTO> detailFields;

        public List<ApiEbFormFieldDTO> getMainFields() {
            return mainFields;
        }

        public void setMainFields(List<ApiEbFormFieldDTO> mainFields) {
            this.mainFields = mainFields;
        }

        public List<ApiEbFormFieldDTO> getDetailFields() {
            return detailFields;
        }

        public void setDetailFields(List<ApiEbFormFieldDTO> detailFields) {
            this.detailFields = detailFields;
        }
    }

    //EB表单字段
    public static class ApiEbFormFieldDTO{
        private Long id;
        private String fieldId;
        private String fieldName;
        private String fieldType;
        private String fieldKey;
        private Integer number;
        private Boolean multiSelect;
        private String objId;

        //是否是EB表单的系统自带字段
        public boolean readIsSystemFieldId(){
            boolean isSystemField = "name".equalsIgnoreCase(fieldId) ||
                    "creator".equalsIgnoreCase(fieldId) ||
                    "create_time".equalsIgnoreCase(fieldId) ||
                    "update_time".equalsIgnoreCase(fieldId) ||
                    "id".equalsIgnoreCase(fieldId) ||
                    "updater".equalsIgnoreCase(fieldId) ||
                    "data_status".equalsIgnoreCase(fieldId) ||
                    "flow_status".equalsIgnoreCase(fieldId) ||
                    "current_step".equalsIgnoreCase(fieldId) ||
                    "flow_system_number".equalsIgnoreCase(fieldId);
            return isSystemField;
        }

        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public String getFieldId() {
            return fieldId;
        }

        public void setFieldId(String fieldId) {
            this.fieldId = fieldId;
        }

        public String getFieldName() {
            return fieldName;
        }

        public void setFieldName(String fieldName) {
            this.fieldName = fieldName;
        }

        public String getFieldType() {
            return fieldType;
        }

        public void setFieldType(String fieldType) {
            this.fieldType = fieldType;
        }

        public String getFieldKey() {
            return fieldKey;
        }

        public void setFieldKey(String fieldKey) {
            this.fieldKey = fieldKey;
        }

        public Integer getNumber() {
            return number;
        }

        public void setNumber(Integer number) {
            this.number = number;
        }

        public Boolean getMultiSelect() {
            return multiSelect;
        }

        public void setMultiSelect(Boolean multiSelect) {
            this.multiSelect = multiSelect;
        }

        public String getObjId() {
            return objId;
        }

        public void setObjId(String objId) {
            this.objId = objId;
        }

        //        {
//            "id": 887060121427460097,
//                "fieldId": "887060121427460097",
//                "fieldName": "拟稿人",
//                "fieldType": "String",
//                "fieldKey": "Text",
//                "number": 3,
//                "multiSelect": false,
//                "objId": 887057840673996801
//        },
    }
    public static class ApiAccessTokenDTO{
        //{"errcode":"0","errmsg":"success","code":"0034b0d8cf14e8623084ed2430850c89"}
        //{"errcode":"0","errmsg":"success","accessToken":"ab2b8cfe6b9ca5ca07e5b1379f6b82f0","acessToken":"ab2b8cfe6b9ca5ca07e5b1379f6b82f0","refreshToken":"0e2693e9ce74378cca2742606bd35a25","expires_in":7200}

        public boolean readIsSuccess(){
            return "0".equals(errcode);
        }
        private String errcode;
        private String errmsg;
        private String code;
        private String accessToken;
        private String acessToken;
        private String refreshToken;
        private Integer expires_in;

        public String getErrcode() {
            return errcode;
        }

        public void setErrcode(String errcode) {
            this.errcode = errcode;
        }

        public String getErrmsg() {
            return errmsg;
        }

        public void setErrmsg(String errmsg) {
            this.errmsg = errmsg;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getAccessToken() {
            return accessToken;
        }

        public void setAccessToken(String accessToken) {
            this.accessToken = accessToken;
        }

        public String getAcessToken() {
            return acessToken;
        }

        public void setAcessToken(String acessToken) {
            this.acessToken = acessToken;
        }

        public String getRefreshToken() {
            return refreshToken;
        }

        public void setRefreshToken(String refreshToken) {
            this.refreshToken = refreshToken;
        }

        public Integer getExpires_in() {
            return expires_in;
        }

        public void setExpires_in(Integer expires_in) {
            this.expires_in = expires_in;
        }
    }

    public static class DbTableDTO{

        public DbTableDTO(){

        }
        public DbTableDTO(String name, String comment){
            this.name = name;
            this.comment = comment;
        }

        private String name;
        private String comment;
        private Map<String, DbTableFieldDTO> fieldMap;

        public String getName(){
            return this.name;
        }
        public void setName(String name){
            this.name = name;
        }
        public String getComment(){
            return this.comment;
        }
        public void setComment(String comment){
            this.comment = comment;
        }
        public Map<String, DbTableFieldDTO> getFieldMap(){
            return this.fieldMap;
        }
        public void setFieldMap(Map<String, DbTableFieldDTO> fieldMap){
            this.fieldMap = fieldMap;
        }
        public DbTableDTO appendField(DbTableFieldDTO fieldDTO){
            if(this.fieldMap == null){
                this.fieldMap = new HashMap<String, DbTableFieldDTO>();
            }
            this.fieldMap.put(fieldDTO.getName(), fieldDTO);
            return this;
        }

    }

    static class JsonHelper {
        public static boolean isNullOrEmpty(Object s){
            return s == null || "".equals(s);
        }
        /**
         * java 对象转Json字符串
         *
         * @param javaObject
         * @return
         */
        public static String parseObjectToJsonString(Object javaObject) {
            if (javaObject == null) {
                return null;
            }
            try {
//			byte[] jsonBytes = JSONObject.toJSONBytes(javaObject);
//			String s = new String(jsonBytes);
                String s = ((JSONObject) JSONObject.toJSON(javaObject)).toJSONString();
                return s;
            } catch (Exception e) {
                e.printStackTrace();
                //GolomeeLogger.exception(e.getStackTrace(), e.getMessage());
                return null;
            }
        }

        public static <T> T parseJsonStringToObject(String key, String jsonStr, Class<T> clazz) {
            if (jsonStr == null) {
                return null;
            }
            try {
                return JSON.parseObject(jsonStr, clazz);
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("parseJsonStringToObject::" + key + "::" + jsonStr);
            }
            return null;
        }

        public static <T> T parseJsonStringToObject(String jsonStr, Class<T> clazz) {
            if (isNullOrEmpty(jsonStr)) {
                return null;
            }
            try {
                return JSON.parseObject(jsonStr, clazz);
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("parseJsonStringToObject::" + jsonStr);
            }
            return null;
        }

        public static <T> List<T> parseJsonStringToListObject(String jsonStr, Class<T> clazz) {
            if (isNullOrEmpty(jsonStr)) {
                return null;
            }
            try {
                return JSONArray.parseArray(jsonStr, clazz);
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("parseJsonStringToListObject::" + jsonStr);
            }
            return null;
        }

        /**
         * java 数组或列表转Json字符串
         *
         * @param javaObject
         * @return
         */
        public static String parseListToJsonString(Object javaObject) {
            if (javaObject == null) {
                return null;
            }
            try {
                String s = ((JSONArray) JSONArray.toJSON(javaObject)).toJSONString();
                return s;
            } catch (Exception e) {
                e.toString();
                return null;
            }
        }
    }
}
